package cn.wolfcode.mycat.util;

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;
import java.io.IOException;

/**
 * JGit API测试
 */
public class GitUtils {

    public String reUrl = "http://user@10.1.2.1:8080/project.git";//远程库路径
    public String locPath = "D:\\project\\";//下载已有仓库到本地路径
    public String initPath = "D:\\test\\";//本地路径新建
	
	public static void main(String[] args) throws IOException, GitAPIException {
		//下面的证明是成功的
		GitUtils gitUtils = new GitUtils();
		
		//不需要密码的指定分支更新
		/*gitUtils.gitPull("C:\\Users\\Nica9\\Desktop\\2222", "dev");
		
		gitUtils.gitPull("C:\\Users\\Nica9\\Pictures\\hcg_code", "xxx@163.com", "xxx", "master");*/
		
		String remoteUrl = "*******";
		boolean b = GitUtils.testClone("username", "password", "master", "D:\\ztProjects\\KZC_BigData\\flink_cdc", remoteUrl);
		System.out.println("====" + b);
	}


    /**
     * 克隆远程库
     *
     * @throws IOException
     * @throws GitAPIException
     */

    public static Boolean testClone(String username, String password, String branch, String localPath, String remoteUrl) throws IOException, GitAPIException {
        //设置远程服务器上的用户名和密码
        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider = new
                UsernamePasswordCredentialsProvider(username, password);

        //克隆代码库命令
        CloneCommand cloneCommand = Git.cloneRepository();

        Git git = cloneCommand.setURI(remoteUrl) //设置远程URI
                .setBranch("master") //设置clone下来的分支
                .setDirectory(new File(localPath)) //设置下载存放路径
                .setCredentialsProvider(usernamePasswordCredentialsProvider) //设置权限验证
                .call();
        //打开连接后一定要关闭，否则一个git文件夹删除不了
        git.close();
        return true;
    }

    public static Boolean testClone(String branch, String localPath, String remoteUrl) throws IOException, GitAPIException {


        //克隆代码库命令
        CloneCommand cloneCommand = Git.cloneRepository();

        Git git = cloneCommand.setURI(remoteUrl) //设置远程URI
                .setBranch("master") //设置clone下来的分支
                .setDirectory(new File(localPath)) //设置下载存放路径
                .call();
 //打开连接后一定要关闭，否则一个git文件夹删除不了
        git.close();
        return true;
    }

    /**
     * 本地新建仓库
     */

    public void testCreate() throws IOException {
        //本地新建仓库地址
        Repository newRepo = FileRepositoryBuilder.create(new File(initPath + "/.git"));
        newRepo.create();
    }

    /**
     * 本地仓库新增文件
     */

    public void testAdd(String localPath) throws IOException, GitAPIException {
        File myfile = new File(localPath + "/myfile.txt");
        myfile.createNewFile();
        //git仓库地址
        Git git = new Git(new FileRepository(localPath + "/.git"));

        //添加文件
        git.add().addFilepattern("myfile").call();
    }

    /**
     * 本地提交代码
     */

    public void testCommit(String localPath) throws IOException, GitAPIException,
            JGitInternalException {
        //git仓库地址
        Git git = new Git(new FileRepository(localPath + "/.git"));
        //提交代码
        git.commit().setMessage("test jGit").call();
    }


    /**
     * 拉取远程仓库内容到本地,不需要账号和密码的,但是需要指定分支
     */

    public void gitPull(String localPath, String branch) throws IOException, GitAPIException {
        //git仓库地址
        Repository repository = new FileRepository(localPath + "/.git");
        Git git = new Git(repository);
        git.pull().setRemoteBranchName(branch).call();
    }


    /**
     * 拉取远程仓库内容到本地,需要账号和密码的
     */

    public void gitPull(String localPath, String username, String password, String branch) throws IOException, GitAPIException {
        UsernamePasswordCredentialsProvider provider = new
                UsernamePasswordCredentialsProvider(username, password);
        //git仓库地址
        Repository repository = new FileRepository(localPath + "/.git");
        Git git = new Git(repository);
        git.pull().setRemoteBranchName(branch).setCredentialsProvider(provider).call();
    }


    /**
     * push本地代码到远程仓库地址
     */

    public void testPush(String localPath) throws IOException, JGitInternalException,
            GitAPIException {

        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider = new
                UsernamePasswordCredentialsProvider("username", "password");
        //git仓库地址
        Git git = new Git(new FileRepository(localPath + "/.git"));
        git.push().setRemote("origin").setCredentialsProvider(usernamePasswordCredentialsProvider).call();
    }


}
